﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
//using Model;

/// <summary>
/// Description résumée de ProgrammeManager
/// </summary>
/// 
namespace DAL
{
    public class ProgrammeManager : IProgrammeManager
    {
        private IAATOEntities dbService;

        public ProgrammeManager()
        {
            dbService = new IAATOEntities();
        }

        public bool addProgramme(PROGRAMME programme)
        {
            try
            {
                dbService.PROGRAMMEJeu.AddObject(programme);
                dbService.SaveChanges();

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool deleteProgramme(int programmeId)
        {
            try
            {
                PROGRAMME programme = (from p in dbService.PROGRAMMEJeu
                             where p.ID == programmeId
                             select p).First();

                dbService.DeleteObject(programme);
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }

        }

        public bool updateProgramme()
        {
            try
            {
                dbService.SaveChanges();
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        public PROGRAMME getProgrammeById(int id)
        {
            try
            {
                return (from p in dbService.PROGRAMMEJeu
                        where p.ID == id
                        select p).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<PROGRAMME> getAllProgrammes()
        {
            try
            {
                return (from p in dbService.PROGRAMMEJeu
                        orderby p.DATE
                        select p).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public PROGRAMME getProgrammeForNavireAtDate(int navireId, DateTime date)
        {
            try
            {
                return (from p in dbService.PROGRAMMEJeu
                        where p.NAVIREID == navireId && p.DATE == date
                        select p).First();
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public List<PROGRAMME> getProgrammesAtDate(DateTime date)
        {
            try
            {
                return (from p in dbService.PROGRAMMEJeu
                        where p.DATE == date
                        select p).ToList();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}